// pages/my/signManage/startSign/startSign.js
var util = require('../../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    signModeArray: [{
      'key': '请选择',
      'value': '0'
    }, {
      'key': '手绘签署',
      'value': '1'
    }, {
      'key': '印章签署',
      'value': '2'
    }], // 签署方式集合
    file: '', // 合同信息
    sealList: '', // 发起人印章集合
    authType: '', // 发起人认证类型
    signList: [], // 签署人集合
    count: '', // 发起人是否已在签署中
    signType: '0', // 签署方式
    mode_name: '请选择', // 签署方式文本
    isDefault: 1, // 发起方
    seal_name: '请选择', // 印章名称
    defaultState: false, // 甲方签署信息
    seal_id: 0, // 印章id
    sealUrl: '', // 印章URL
    moreSignMan: [], // 更多签署方
    sign_keyword: '', //签署关键字
    eseal_id: '', // e签宝印章id
    auth_name: '', // 签署方名称
    auth_code: '', // 签署方代码
    disabledBtn: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        var data = res.data;
        that.setData({
          userInfo: data,
          file_id: options.id
        });
        that.dataLoad();
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 初始化数据
   */
  dataLoad: function() {
    wx.showLoading({
      title: '加载中···',
    });
    var that = this;
    var file_id = that.data.file_id;
    util.getReq('/app/sign/getFileSign', {
      file_id: file_id
    }, function(data) {
      if (data.code == 0) {
        var params = data.params;
        that.setData({
          count: params.count,
          file: params.file,
          sealList: params.sealList,
          signList: params.signList,
          auth_name: '',
          auth_code: '',
        });

        // 企业认证只允许印章签署
        if (params.authType == 1) {
          that.data.signModeArray.splice(1, 1);
          that.setData({
            signModeArray: that.data.signModeArray
          });
        }
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  /**
   * 发起方默认为甲方签署开关
   */
  switchChange: function(e) {
    var that = this;
    if (!e.detail.value) {
      that.setData({
        defaultState: true,
        isDefault: 0
      });
    } else {
      that.setData({
        defaultState: false,
        isDefault: 1
      });
    }
  },

  /**
   * 签署关键字
   */
  sign_keyword: function(e) {
    this.setData({
      sign_keyword: e.detail.value
    });
  },

  /**
   * 签署方名称
   */
  auth_name: function(e) {
    this.setData({
      auth_name: e.detail.value
    });
  },

  /**
   * 签署方代码
   */
  auth_code: function(e) {
    this.setData({
      auth_code: e.detail.value
    });
  },

  /**
   * 签署方式选择
   */
  bindSignModeChange: function(e) {
    var that = this;
    var data = that.data.signModeArray[e.detail.value];
    that.setData({
      signType: data.value,
      mode_name: data.key
    });
  },

  /**
   * 印章选择
   */
  bindSealChange: function(e) {
    var that = this;
    var data = that.data.sealList[e.detail.value];
    that.setData({
      seal_id: data.seal_id,
      seal_name: data.seal_name,
      eseal_id: data.eseal_id,
      sealUrl: data.preview_url
    });
  },

  /**
   * 添加更多
   */
  addMore: function() {
    var that = this;
    var moreSignMan = that.data.moreSignMan;
    moreSignMan.push(moreSignMan.length + 1);
    var auth_name = 'auth_name' + moreSignMan.length;
    var auth_code = 'auth_code' + moreSignMan.length;
    that.setData({
      moreSignMan: moreSignMan,
      [auth_name]: '',
      [auth_code]: ''
    });
    that[auth_name] = function(e) {
      that.setData({
        [auth_name]: e.detail.value
      });
    };
    that[auth_code] = function(e) {
      that.setData({
        [auth_code]: e.detail.value
      });
    }
  },

  /**
   * 移除
   */
  removeSignMan: function(e) {
    var dataset = e.target.dataset;
    var idx = dataset.idx;
    var moreSignMan = this.data.moreSignMan;
    moreSignMan.splice(idx, 1);
    this.setData({
      moreSignMan: moreSignMan
    });
  },

  submit: function() {
    var that = this;
    if (!that.submitCheck()) {
      return;
    }
    that.setData({
      disabledBtn: true
    });
    wx.showLoading({
      title: '提交中···',
    })
    var data = that.data;
    var list = data.moreSignMan;
    var params = {
      'file_id': data.file_id,
      'isDefault': data.isDefault,
      'eSign.sign_type': data.signType,
      'eSign.sign_keyword': data.sign_keyword,
      'eSign.seal_id': data.seal_id,
      'eSign.eseal_id': data.eseal_id
    };
    var signJson = [{
      'auth_name': data.auth_name,
      'auth_code': data.auth_code
    }];
    for (var i = 0; i < list.length; i++) {
      var auth_name = 'auth_name' + list[i];
      var auth_code = 'auth_code' + list[i];
      if (that.data[auth_name] == '' || that.data[auth_code] == '') {
        util.isError('请输入签署方认证信息');
        return;
      }
      var json = {
        'auth_name': that.data[auth_name],
        'auth_code': that.data[auth_code]
      };
      signJson.push(json);
    };
    params.signJson = JSON.stringify(signJson);
    if (signJson.length + data.isDefault < 2 && data.signList.length == 0) {
      util.isError('签署方人数不能少于2人!');
      return;
    }
    util.getReq('/app/sign/startSign', params, function(data) {
      if (data.code == 0) {
        wx.showToast({
          title: '提交成功',
        });
        that.dataLoad();
      } else {
        util.isError(data.desc);
        wx.hideLoading();
      }
      that.setData({
        disabledBtn: false
      });
    });
  },

  /**
   * 提交参数校验
   */
  submitCheck: function() {
    var that = this;
    var data = that.data;
    if (data.signType == 0 && !data.defaultState && data.count == 0) {
      util.isError('请选择签署方式');
      return false;
    }
    if (data.sign_keyword == '' && !data.defaultState && data.count == 0) {
      util.isError('请输入签署关键字');
      return false;
    }
    if (data.signType == 2 && !data.defaultState && data.seal_id == 0 && data.count == 0) {
      util.isError('请选择签署印章');
      return false;
    }
    if (data.auth_name == '' || data.auth_code == '') {
      util.isError('请输入签署方认证信息');
      return false;
    }
    return true;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.dataLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})